package com.monkey.service;

import com.monkey.core.ErrorCode;
import com.monkey.generator.dao.TMonkeyCreateMapper;
import com.monkey.generator.dao.TRewardMapper;
import com.monkey.generator.model.TMonkeyCreate;
import com.monkey.generator.model.TReward;
import com.monkey.generator.model.TRewardExample;
import com.monkey.model.monkey.Monkey;
import com.monkey.model.monkey.MonkeyCreate;
import com.monkey.model.monkey.MonkeySpecify;
import com.monkey.model.reward.RewardData;
import com.monkey.model.reward.RewardItemData;
import com.monkey.model.reward.RewardListData;
import com.monkey.model.user.User;
import com.monkey.util.DateUtil;
import com.monkey.util.WebConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.monkey.service.MonkeyService.MONKEY_FROM_ADMIN;
import static com.monkey.service.UserService.ACTION_ADMIN;

/**
 * desc:
 * auth: chenxiaobin
 * data: 2018/2/3.
 */
@Service
@Transactional
public class RewardService {
    private static Logger logger = LoggerFactory.getLogger(RewardService.class);
    private static final int REWARD_MONKEY = 1; // 送宠物
    private static final int REWARD_GOLD = 2; // 送金币

    @Resource
    private UserService userService = null;

    @Resource
    private MonkeyService monkeyService = null;

    @Resource
    private TMonkeyCreateMapper monkeyCreateMapper = null;

    @Resource
    private TRewardMapper rewardMapper = null;

    @Resource
    private DateUtil dateUtil = null;

    @Resource
    private WebConfig webConfig = null;

    public RewardData sendReward(String value) {
        String[] cmds = value.split(";");
        if (0 == cmds.length) {
            return new RewardData(ErrorCode.PARAM_ERROR, "内容为空");
        }
        // 只校验不执行
        int idBase = webConfig.getInt("monkey.id.start");
        for (int i = 0; i < cmds.length; ++i) {
            String[] strs = cmds[i].split(",");
            if (strs.length < 2) {
                return new RewardData(ErrorCode.PARAM_ERROR, "第" + (i + 1) + "行参数太少");
            }
            User user = userService.getUser(strs[0]);
            if (null == user) {
                return new RewardData(ErrorCode.USER_NO_EXIST, "第" + (i + 1) + "行用户不存在");
            }
            switch (Integer.valueOf(strs[1])) {
                case REWARD_MONKEY:
                    if (9 != strs.length) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行参数需要9个");
                    }
                    int id = Integer.valueOf(strs[2]);
                    int generation = Integer.valueOf(strs[3]);
                    int grow = Integer.valueOf(strs[6]);
                    int reproduct = Integer.valueOf(strs[7]);
                    int bonus = Integer.valueOf(strs[8]);
                    int uuid1 = Integer.valueOf(strs[4]);
                    int uuid2 = Integer.valueOf(strs[5]);
                    if (id < 0) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物id小于0");
                    }
                    if (id >= idBase) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物id大于" + idBase);
                    }
                    if (null != monkeyService.getMonkey(Integer.valueOf(strs[2]))) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物id已经存在");
                    }
                    if (generation < 0) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物代数小于0");
                    }
                    if (grow < 100) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物成长小于1");
                    }
                    if (reproduct < 100) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物生育小于1");
                    }
                    if (bonus < 100) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物掘金小于1");
                    }
                    if (uuid1 < 0) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物品种小于0");
                    }
                    if (uuid2 < 0) {
                        return new RewardData(ErrorCode.REWARD_MONKEY_FAIL, "第" + (i + 1) + "行宠物套装小于0");
                    }
                    break;

                case REWARD_GOLD:
                    if (3 != strs.length) {
                        return new RewardData(ErrorCode.REWARD_GOLD_FAIL, "第" + (i + 1) + "行参数需要3个");
                    }
                    break;

                default:
                    break;
            }
        }

        // 正式执行
        Date now = new Date();
        for (int i = 0; i < cmds.length; ++i) {
            String[] strs = cmds[i].split(",");
            User user = userService.getUser(strs[0]);
            if (null == user) {
                logger.info("send reward user error:{}", cmds[i]);
                continue;
            }

            TReward reward = new TReward();
            reward.setUserid(user.getId());
            reward.setItem(cmds[i]);
            reward.setOperater(0);
            reward.setCreatetime(now);

            switch (Integer.valueOf(strs[1])) {
                case REWARD_MONKEY:
                    int id = Integer.valueOf(strs[2]);
                    int generation = Integer.valueOf(strs[3]);
                    int grow = Integer.valueOf(strs[6]);
                    int reproduct = Integer.valueOf(strs[7]);
                    int bonus = Integer.valueOf(strs[8]);
                    int uuid1 = Integer.valueOf(strs[4]);
                    int uuid2 = Integer.valueOf(strs[5]);
                    MonkeyCreate create = monkeyService.createData(grow % 100 / 10, reproduct % 100 / 10, bonus % 100 / 10);
                    MonkeySpecify specify = new MonkeySpecify(id, grow, reproduct, bonus, uuid2);
                    Monkey monkey = monkeyService.create(generation, 0, 0, user, MONKEY_FROM_ADMIN, uuid1, create, specify);
                    if (null == monkey) {
                        logger.info("send reward monkey error:{}", cmds[i]);
                        continue;
                    }

                    //
                    TMonkeyCreate create1 = new TMonkeyCreate();
                    create1.setMonkeyid(monkey.getId());
                    create1.setUuid(create.getUuid());
                    create1.setCreatetime(new Date());
                    if (0 == monkeyCreateMapper.insert(create1)) {
                        logger.info("monkey create db error:{}, {}", monkey.getId(), create.getUuid());
                    }

                    // 记录
                    reward.setItemtype(REWARD_MONKEY);
                    if (0 == rewardMapper.insert(reward)) {
                        logger.info("send reward monkey db error:{}", cmds[i]);
                        continue;
                    }
                    logger.info("send reward monkey success:{}, {}, {}", strs[0], user.getId(), monkey.getId());
                    break;

                case REWARD_GOLD:
                    userService.modifyGold(strs[0], Float.valueOf(strs[2]), ACTION_ADMIN);

                    // 记录
                    reward.setItemtype(REWARD_GOLD);
                    if (0 == rewardMapper.insert(reward)) {
                        logger.info("send reward monkey db error:{}", cmds[i]);
                        continue;
                    }
                    logger.info("send reward gold success:{}, {}, {}", strs[0], user.getId(), strs[2]);
                    break;

                default:
                    break;
            }
        }
        return new RewardData(ErrorCode.SUCCESS, "发奖成功");
    }

    public RewardListData getRewardList(int offset, int num) {
        TRewardExample example = new TRewardExample();
        int total = (int) rewardMapper.countByExample(example);

        SimpleDateFormat sdf = dateUtil.getDateFormat();
        example = new TRewardExample();
        example.setOffset(offset);
        example.setLimit(num);
        example.setOrderByClause("createTime DESC");
        List<TReward> list = rewardMapper.selectByExample(example);
        List<RewardItemData> list1 = new ArrayList<>();
        for (TReward reward : list) {
            list1.add(new RewardItemData(reward, sdf));
        }
        return new RewardListData(ErrorCode.SUCCESS, total, list1);
    }
}
